/**
 * @author : 乔磊磊
 * @ide    : WebStorm
 * @date   : 2025/2/22 星期六 22:42
 * @desc   :
 */

export class StrAndNumUtil {
    private static lastTimestamp = 0;
    private static counter = 0;
    private static readonly SECRET_KEY = "YourSecretKeyHere"; // 请替换为你的秘密密钥
    private static readonly SALT = "SomeSaltValue"; // 添加一些"盐"来增加复杂度

    /**
     * 返回指定小数位的数字
     */
    public static toFixedNumber(num: number, decimalPlaces: number): number {
        return Number(num.toFixed(decimalPlaces));
    }

    /**
     * 格式化数字为货币格式
     */
    public static toCurrency(num: number, decimalPlaces: number = 2): string {
        return this.toFixedNumber(num, decimalPlaces).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
    }

    /**
     * 格式化数字为百分比格式
     */
    public static toPercentage(num: number, decimalPlaces: number = 2): string {
        return this.toFixedNumber(num * 100, decimalPlaces).toString() + '%';
    }

    /**
     * 格式化大数字为简洁格式，例如：1.23K, 4.56M
     */
    public static formatLargeNumber(num: number): string {
        const units = ["", "K", "M", "B", "T", "P", "E"];
        const threshold = 1000;

        if (num < threshold) {
            return num.toString();
        }

        let unitIndex = 0;
        let formattedNumber = num;

        while (formattedNumber >= threshold) {
            formattedNumber /= threshold
            unitIndex++
        }
        const unit = units[unitIndex - 1] || "U"

        // while (formattedNumber >= threshold && unitIndex < units.length - 1) {
        //     formattedNumber /= threshold;
        //     unitIndex++;
        // }

        return formattedNumber.toFixed(2) + unit;
    }

    /**
     * 获取一个不重复的 ID
     * @param {number} length
     * @returns {string}
     */
    public static uniqueID(length: number = 16): string {
        const timestamp = Date.now();
        if (timestamp === this.lastTimestamp) {
            this.counter++;
        } else {
            this.lastTimestamp = timestamp;
            this.counter = 0;
        }

        const timestampStr = timestamp.toString(36);
        const counterStr = ('00' + this.counter.toString(36)).slice(-2);
        const randomStr = Math.random().toString(36).slice(2, 9);

        const id = timestampStr + counterStr + randomStr;
        return id.slice(0, length);
    }

    /**
     * 加密字符串
     */
    public static encryptString(str: string): string {
        let encrypted = '';
        const saltedStr = str + this.SALT;
        for (let i = 0; i < saltedStr.length; i++) {
            const charCode = saltedStr.charCodeAt(i);
            const keyChar = this.SECRET_KEY.charCodeAt(i % this.SECRET_KEY.length);
            const encryptedCharCode = ((charCode + keyChar) % 256) ^ keyChar;
            encrypted += String.fromCharCode(encryptedCharCode);
        }
        return btoa(encrypted); // 使用Base64编码
    }

    /**
     * 解密字符串
     */
    public static decryptString(encryptedStr: string): string {
        let decrypted = '';
        const decodedStr = atob(encryptedStr); // 解码Base64
        for (let i = 0; i < decodedStr.length; i++) {
            const charCode = decodedStr.charCodeAt(i);
            const keyChar = this.SECRET_KEY.charCodeAt(i % this.SECRET_KEY.length);
            const decryptedCharCode = ((charCode ^ keyChar) + 256 - keyChar) % 256;
            decrypted += String.fromCharCode(decryptedCharCode);
        }
        return decrypted.slice(0, -this.SALT.length); // 移除添加的"盐"
    }
}
